package com.tianma.sync;

import com.tianma.entity.TradeInfo;
import com.tianma.model.BinlogValue;
import com.tianma.service.TradeInfoService;
import com.tianma.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class TradeInfoSync extends AbstractSyncTemplate {
    @Autowired
    private TradeInfoService tradeInfoService;

    @Override
    void insert(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void update(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void delete(List<Map<String, BinlogValue>> rows) {
        for (Map<String, BinlogValue> row : rows) {
            BinlogValue id = row.get("trade_id");
            tradeInfoService.removeById(id.getValue());
            log.debug("删除 trade_info 记录：", id);
        }
    }

    private void saveOrUpdate(List<Map<String, BinlogValue>> rows) throws Exception {
        for (Map<String, BinlogValue> row : rows) {

            TradeInfo trade = new TradeInfo();

            trade.setTradeId(Long.parseLong(row.get("trade_id").getValue()));

            trade.setFxUid(StringUtils.isNotEmpty(row.get("fx_uid").getValue()) ? Long.parseLong(row.get("fx_uid").getValue()) : 0);
            trade.setFxSuid(StringUtils.isNotEmpty(row.get("fx_suid").getValue()) ? Long.parseLong(row.get("fx_suid").getValue()) : 0);
            trade.setFxUsername(row.get("fx_username").getValue());
            trade.setPtUid(StringUtils.isNotEmpty(row.get("pt_uid").getValue()) ? Long.parseLong(row.get("pt_uid").getValue()) : 0);
            trade.setPtUsername(row.get("pt_username").getValue());
            trade.setPlatType(row.get("plat_type").getValue());
            trade.setOuterTid(row.get("outer_tid").getValue());
            trade.setMainOuterTid(row.get("main_outer_tid").getValue());
            trade.setPlatTotalPrice(StringUtils.isNotEmpty(row.get("plat_total_price").getValue()) ? Double.parseDouble(row.get("plat_total_price").getValue()) : 0);
            trade.setPlatPostFee(StringUtils.isNotEmpty(row.get("plat_post_fee").getValue()) ? Double.parseDouble(row.get("plat_post_fee").getValue()) : 0);
            trade.setPayFrom(row.get("pay_from").getValue());
            trade.setShop(row.get("shop").getValue());
            trade.setPayAccount(row.get("pay_account").getValue());
            trade.setTotalPrice(StringUtils.isNotEmpty(row.get("total_price").getValue()) ? Double.parseDouble(row.get("total_price").getValue()) : 0);
            trade.setDiscountPrice(StringUtils.isNotEmpty(row.get("discount_price").getValue()) ? Double.parseDouble(row.get("discount_price").getValue()) : 0);
            trade.setCommissionPrice(StringUtils.isNotEmpty(row.get("commission_price").getValue()) ? Double.parseDouble(row.get("commission_price").getValue()) : 0);
            trade.setAdjustPrice(StringUtils.isNotEmpty(row.get("adjust_price").getValue()) ? Double.parseDouble(row.get("adjust_price").getValue()) : 0);
            trade.setPostFee(StringUtils.isNotEmpty(row.get("post_fee").getValue()) ? Double.parseDouble(row.get("post_fee").getValue()) : 0);
            trade.setRecvPrice(StringUtils.isNotEmpty(row.get("recv_price").getValue()) ? Double.parseDouble(row.get("recv_price").getValue()) : 0);
            trade.setRetPrice(StringUtils.isNotEmpty(row.get("ret_price").getValue()) ? Double.parseDouble(row.get("ret_price").getValue()) : 0);
            trade.setRecvName(row.get("recv_name").getValue());
            trade.setRecvAddress(row.get("recv_address").getValue());

            trade.setRecvTel(row.get("recv_tel").getValue());
            trade.setRecvMobile(row.get("recv_mobile").getValue());
            trade.setProvinceId(StringUtils.isNotEmpty(row.get("province_id").getValue()) ? Integer.parseInt(row.get("province_id").getValue()) : 0);
            trade.setProvince(row.get("province").getValue());
            trade.setCityId(StringUtils.isNotEmpty(row.get("city_id").getValue()) ? Integer.parseInt(row.get("city_id").getValue()) : 0);
            trade.setCity(row.get("city").getValue());
            trade.setAreaId(StringUtils.isNotEmpty(row.get("area_id").getValue()) ? Integer.parseInt(row.get("area_id").getValue()) : 0);
            trade.setArea(row.get("area").getValue());
            trade.setZipcode(row.get("zipcode").getValue());
            trade.setMessage(row.get("message").getValue());
            trade.setSellerRemark(row.get("seller_remark").getValue());
            trade.setStatus(StringUtils.isNotEmpty(row.get("status").getValue()) ? Integer.parseInt(row.get("status").getValue()) : 0);
            trade.setRemarkFlag(StringUtils.isNotEmpty(row.get("remark_flag").getValue()) ? Integer.parseInt(row.get("remark_flag").getValue()) : 0);
            trade.setRemark(row.get("remark").getValue());
            trade.setPromotionNo(row.get("promotion_no").getValue());
            trade.setOrderNum(StringUtils.isNotEmpty(row.get("order_num").getValue()) ? Integer.parseInt(row.get("order_num").getValue()) : 0);
            trade.setOrderDate(DateUtils.parseDate(row.get("order_date").getValue()));
            trade.setIsActivity(StringUtils.isNotEmpty(row.get("is_activity").getValue()) ? Integer.parseInt(row.get("is_activity").getValue()) : 0);
            trade.setIsInvoice(StringUtils.isNotEmpty(row.get("is_invoice").getValue()) ? Integer.parseInt(row.get("is_invoice").getValue()) : 0);
            trade.setCreated(DateUtils.parseDatetime(row.get("created").getValue()));
            trade.setModified(DateUtils.parseDatetime(row.get("modified").getValue()));
            trade.setGroupType(StringUtils.isNotEmpty(row.get("group_type").getValue()) ? Integer.parseInt(row.get("group_type").getValue()) : 0);
            trade.setTradeFrom(row.get("trade_from").getValue());
            trade.setReturnReason(row.get("return_reason").getValue());
            trade.setIsNotify(StringUtils.isNotEmpty(row.get("is_notify").getValue()) ? Integer.parseInt(row.get("is_notify").getValue()) : 0);
            trade.setPNum(StringUtils.isNotEmpty(row.get("p_num").getValue()) ? Integer.parseInt(row.get("p_num").getValue()) : 0);
            trade.setPStatus(StringUtils.isNotEmpty(row.get("p_status").getValue()) ? Integer.parseInt(row.get("p_status").getValue()) : 0);
            trade.setPTime(DateUtils.parseDatetime(row.get("p_time").getValue()));


            log.debug("插入或更新 trade_info 数据:", trade);
            tradeInfoService.saveOrUpdate(trade);
        }
    }


}
